/*
 * ClientSide.java
 *
 * Created on October 17, 2008, 1:12 PM
 */

package com.socket.client;

import java.io.IOException;
import java.net.UnknownHostException;
import java.util.Vector;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.border.BevelBorder;
import com.socket.xml.XMLParser;
import java.net.InetAddress;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
/**
 *
 * @author  CongChinh
 */
public class ClientSide extends JFrame {    
    MotorTableModel model;
    //private Boolean connected = false;
    ClientService clientService;
    /** Creates new form ClientSide */
    public ClientSide() {        
        this.setTitle("[Client]Motor Management - 04T4");
        model = new MotorTableModel();
        initComponents();
        btnDisconnect.setEnabled(false);        
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {                
                if (clientService != null && clientService.is_connected()) {
                close();      
                }
            System.exit(0);
            }
        });
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        btnConnect = new javax.swing.JButton();
        btnDisconnect = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtHost = new javax.swing.JTextField();
        txtPort = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        //create a table with a model parameter :)
    jTable1 = new javax.swing.JTable(model);
    lblError = new javax.swing.JLabel();
    jPanel2 = new javax.swing.JPanel();
    lblStatus = new javax.swing.JLabel();
    txtID = new javax.swing.JTextField();
    txtName = new javax.swing.JTextField();
    jLabel3 = new javax.swing.JLabel();
    jLabel4 = new javax.swing.JLabel();
    jLabel5 = new javax.swing.JLabel();
    jLabel6 = new javax.swing.JLabel();
    jLabel7 = new javax.swing.JLabel();
    txtColor = new javax.swing.JTextField();
    txtPrice = new javax.swing.JTextField();
    jLabel8 = new javax.swing.JLabel();
    txtQuantity = new javax.swing.JTextField();
    btnUpdate = new javax.swing.JButton();
    btnExit = new javax.swing.JButton();
    btnAdd = new javax.swing.JButton();
    btnDelete = new javax.swing.JButton();
    btnRefresh = new javax.swing.JButton();
    lblAction = new javax.swing.JLabel();
    btnFind = new javax.swing.JButton();
    txtMark = new javax.swing.JTextField();
    jMenuBar1 = new javax.swing.JMenuBar();
    mFile = new javax.swing.JMenu();
    miExit = new javax.swing.JMenuItem();
    mEdit = new javax.swing.JMenu();
    mHelp = new javax.swing.JMenu();
    miAbout = new javax.swing.JMenuItem();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jPanel1.setBackground(new java.awt.Color(102, 153, 255));

    btnConnect.setText("Connect");
    btnConnect.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnConnectActionPerformed(evt);
        }
    });

    btnDisconnect.setText("Disconnect");
    btnDisconnect.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnDisconnectActionPerformed(evt);
        }
    });

    jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12));
    jLabel1.setText("Server IP Address:");

    jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12));
    jLabel2.setText("Server Port");

    txtHost.setText("127.0.0.1");
    txtHost.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            txtHostActionPerformed(evt);
        }
    });

    txtPort.setText("1001");

    /*
    jTable1.setModel(model
    );
    */
    JScrollPane pane = new JScrollPane(jTable1);
    pane.setBorder(new BevelBorder(BevelBorder.LOWERED));
    jScrollPane2.setViewportView(jTable1);

    lblError.setFont(new java.awt.Font("Tahoma", 3, 12));
    lblError.setForeground(new java.awt.Color(255, 0, 0));

    javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
    jPanel2.setLayout(jPanel2Layout);
    jPanel2Layout.setHorizontalGroup(
        jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel2Layout.createSequentialGroup()
            .addComponent(lblStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(621, Short.MAX_VALUE))
    );
    jPanel2Layout.setVerticalGroup(
        jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(lblStatus, javax.swing.GroupLayout.DEFAULT_SIZE, 20, Short.MAX_VALUE)
    );

    txtName.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            txtNameActionPerformed(evt);
        }
    });

    jLabel3.setText("Mã xe");

    jLabel4.setText("Tên xe");

    jLabel5.setText("Màu xe");

    jLabel6.setText("Hiệu xe");

    jLabel7.setText("Giá");

    jLabel8.setText("Số lượng");

    btnUpdate.setText("Sửa");
    btnUpdate.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnUpdateActionPerformed(evt);
        }
    });

    btnExit.setText("Thoát");
    btnExit.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnExitActionPerformed(evt);
        }
    });

    btnAdd.setText("Thêm");
    btnAdd.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnAddActionPerformed(evt);
        }
    });

    btnDelete.setText("Xóa");
    btnDelete.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnDeleteActionPerformed(evt);
        }
    });

    btnRefresh.setText("Refresh");
    btnRefresh.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnRefreshActionPerformed(evt);
        }
    });

    lblAction.setFont(new java.awt.Font("Tahoma", 3, 12));
    lblAction.setForeground(new java.awt.Color(255, 0, 0));

    btnFind.setText("Tìm");
    btnFind.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnFindActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(55, 55, 55)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel6)
                                .addComponent(jLabel3)
                                .addComponent(jLabel8)
                                .addComponent(jLabel4)
                                .addComponent(jLabel5)
                                .addComponent(jLabel7))
                            .addGap(28, 28, 28)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(txtMark)
                                .addComponent(txtPrice)
                                .addComponent(txtQuantity)
                                .addComponent(txtID, javax.swing.GroupLayout.DEFAULT_SIZE, 178, Short.MAX_VALUE)
                                .addComponent(txtName)
                                .addComponent(txtColor))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(btnFind))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(btnAdd)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(btnDelete)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(btnUpdate)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(btnExit))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(lblAction, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 580, Short.MAX_VALUE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(btnRefresh))))
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(39, 39, 39)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(lblError, javax.swing.GroupLayout.PREFERRED_SIZE, 586, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel1)
                                .addComponent(jLabel2))
                            .addGap(18, 18, 18)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(txtPort)
                                .addComponent(txtHost, javax.swing.GroupLayout.DEFAULT_SIZE, 133, Short.MAX_VALUE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(btnConnect, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(btnDisconnect, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))))
            .addContainerGap())
        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                    .addGap(16, 16, 16)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(txtHost, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(btnConnect))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(txtPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(btnDisconnect))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(lblError, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(6, 6, 6)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel3)
                        .addComponent(txtID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(btnFind))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel4))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel6)
                        .addComponent(txtMark, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel5)
                        .addComponent(txtColor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtPrice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel7))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtQuantity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel8))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btnAdd)
                        .addComponent(btnDelete)
                        .addComponent(btnUpdate)
                        .addComponent(btnExit))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 215, Short.MAX_VALUE))
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(btnRefresh)))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(lblAction, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(11, 11, 11)
            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    );

    mFile.setText("File");

    miExit.setText("Exit");
    miExit.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            miExitActionPerformed(evt);
        }
    });
    mFile.add(miExit);

    jMenuBar1.add(mFile);

    mEdit.setText("Edit");
    mEdit.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            clickExit(evt);
        }
    });
    jMenuBar1.add(mEdit);

    mHelp.setText("Help");

    miAbout.setText("About");
    mHelp.add(miAbout);

    jMenuBar1.add(mHelp);

    setJMenuBar(jMenuBar1);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );

    pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnConnectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConnectActionPerformed
        try {
            // TODO add your handling code here:
            String host = txtHost.getText();
            InetAddress address = InetAddress.getByName(host);
            int port = Integer.parseInt(txtPort.getText());
            clientService = new ClientService();
            clientService.connect(address, port);         
            connectStatus();
            
            //
            requestShow();            
        } catch (UnknownHostException ex) {
            lblError.setText("Cannot detect the host \"" + txtHost.getText() +"\"");
        } catch (IOException ex) {
            lblError.setText("Cannot connect to server. Server is not available.");
        }  
    }//GEN-LAST:event_btnConnectActionPerformed
    
    private void txtHostActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtHostActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_txtHostActionPerformed

    private void connectStatus() {
        lblError.setText("");
        btnConnect.setEnabled(false);
        btnDisconnect.setEnabled(true);
        lblAction.setText("");
        lblStatus.setText("Connected..");        
        clientService.setConnectStatus(true);
    }
    private void disconnectStatus() {
        model.setMotorsModel(new Vector());
        lblError.setText("");
        btnConnect.setEnabled(true);
        btnDisconnect.setEnabled(false);
        lblAction.setText("");
        lblStatus.setText("Disconnected..");
        clientService.setConnectStatus(false);
    }
    private Boolean validateInput() {
        if (txtID.getText().equals("") ||
            txtName.getText().equals("") ||
            txtColor.getText().equals("") ||
            txtPrice.getText().equals("") ||
            txtMark.getText().equals("") ||
            txtQuantity.getText().equals(""))
        {
            lblAction.setText("All fields are required.");
            return false;
        }
        return  true;
    }
    private Boolean validateNumberInput() {
        Boolean checkDouble = true;
        Boolean checkInt = true;
        //
        try {
            double d = Double.parseDouble(txtPrice.getText());
        }
        catch (Exception e) {
            lblAction.setText("Price must be a number.");
            checkDouble = false;
        }
        //
        try {
            int i = Integer.parseInt(txtQuantity.getText());
        }        
        catch (Exception e) {
            lblAction.setText("Quantity must be a integer.");
            checkInt = false;
        }
        //
        if (checkDouble && checkInt)
            return true;
        else 
            return false;
    }
    private void btnDisconnectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDisconnectActionPerformed
        // TODO add your handling code here:        
        this.close();
        disconnectStatus();      
    }//GEN-LAST:event_btnDisconnectActionPerformed

    private void clickExit(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clickExit
        // TODO add your handling code here:
    }//GEN-LAST:event_clickExit

    private void miExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_miExitActionPerformed
        // TODO add your handling code here:
        if (clientService != null && clientService.is_connected()) {
            this.close();
        }
        System.exit(0);
    }//GEN-LAST:event_miExitActionPerformed

    private void txtNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtNameActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_txtNameActionPerformed

    private void requestShow() {
        try {
            String xmlRequest;
            XMLParser xml = new XMLParser();

            xmlRequest = xml.openTag("query");
            xmlRequest += xml.newItem("action", "SHOW");
            xmlRequest += xml.closeTag("query");
            
            //call client's service - send()
            clientService.send(xmlRequest);
            
            //receive response returned from Server
            String xmlResponse = clientService.receive();
            xml.parserXMLString(xmlResponse);
            int size = Integer.parseInt(xml.getValue("size", 0));

            Vector rows = new Vector();
            for (int i = 0; i < size; i++) {
                Vector row = new Vector();
                row.add(xml.getValue("id", i));
                row.add(xml.getValue("name", i));
                row.add(xml.getValue("color", i));
                row.add(xml.getValue("price", i));
                row.add(xml.getValue("mark", i));
                row.add(xml.getValue("quantity", i));
                rows.add(row);
            }
            //set table model
            model.setMotorsModel(rows);
        } catch (SAXException ex) {
            lblError.setText(ex.getMessage());
        } catch (IOException ex) {
            lblError.setText(ex.getMessage());
        } catch (ParserConfigurationException ex) {
            lblError.setText(ex.getMessage());
        }
    }
    
    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        // TODO add your handling code here:
        if (validateInput() && validateNumberInput())
        {
            String xmlRequest;
            XMLParser xml = new XMLParser(); 

            xmlRequest  = xml.openTag("query");
            xmlRequest +=   xml.newItem("action","ADD");
            xmlRequest +=   xml.openTag("data");
            xmlRequest +=       xml.openTag("motor");
            xmlRequest +=           xml.newItem("id",txtID.getText());
            xmlRequest +=           xml.newItem("name",txtName.getText());
            xmlRequest +=           xml.newItem("color",txtColor.getText());
            xmlRequest +=           xml.newItem("price",txtPrice.getText());
            xmlRequest +=           xml.newItem("mark",txtMark.getText());
            xmlRequest +=           xml.newItem("quantity",txtQuantity.getText());
            xmlRequest +=       xml.closeTag("motor");
            xmlRequest +=   xml.closeTag("data");
            xmlRequest += xml.closeTag("query");
            
            //call client's service - send()
            clientService.send(xmlRequest);

            //read data returned from Server
            try {
                //call client's service - receive()
                String xmlResponse = clientService.receive();            
                xml.parserXMLString(xmlResponse);
                
                if (xml.getValue("status").equals("ok")) {            
                    lblAction.setText("Add ok.");
                    requestShow();
                } //"error"
                else {
                    lblAction.setText("Add fail.");
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }                      
    }//GEN-LAST:event_btnAddActionPerformed

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        // TODO add your handling code here:
        if (!txtID.getText().equals("")) 
        {
            String xmlRequest;
            XMLParser xml = new XMLParser();

            xmlRequest  = xml.openTag("query");
            xmlRequest +=   xml.newItem("action","DELETE");
            xmlRequest +=   xml.openTag("data");
            xmlRequest +=       xml.openTag("motor");
            xmlRequest +=           xml.newItem("id",txtID.getText());
            xmlRequest +=       xml.closeTag("motor");
            xmlRequest +=   xml.closeTag("data");
            xmlRequest += xml.closeTag("query");
            
            //call client's service - send()
            clientService.send(xmlRequest);

            //read data returned from Server
            try {
                //call client's service - receive()
                String xmlResponse = clientService.receive();                
                xml.parserXMLString(xmlResponse);
                
                if (xml.getValue("status").equals("ok")) {                
                    lblAction.setText("Delete ok.");
                    requestShow();
                } //"error"
                else {
                    lblAction.setText("Delete fail.");
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        else 
        {
            lblAction.setText("Motor ID is required.");
        }
    }//GEN-LAST:event_btnDeleteActionPerformed

    private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateActionPerformed
        // TODO add your handling code here:        
        if (validateInput())
        {
            String xmlRequest;        
            XMLParser xml = new XMLParser(); 

            xmlRequest  = xml.openTag("query");
            xmlRequest +=   xml.newItem("action","UPDATE");
            xmlRequest +=   xml.openTag("data");
            xmlRequest +=       xml.openTag("motor");
            xmlRequest +=           xml.newItem("id",txtID.getText());
            xmlRequest +=           xml.newItem("name",txtName.getText());
            xmlRequest +=           xml.newItem("color",txtColor.getText());
            xmlRequest +=           xml.newItem("price",txtPrice.getText());
            xmlRequest +=           xml.newItem("mark",txtMark.getText());
            xmlRequest +=           xml.newItem("quantity",txtQuantity.getText());
            xmlRequest +=       xml.closeTag("motor");
            xmlRequest +=   xml.closeTag("data");
            xmlRequest += xml.closeTag("query");
            
            //call client's service - send()
            clientService.send(xmlRequest);

            //read data returned from Server
            try {
                //call client's service - receive()
                String xmlResponse = clientService.receive();            
                xml.parserXMLString(xmlResponse);
                
                if (xml.getValue("status").equals("ok")) {                
                    lblAction.setText("Update ok.");
                    requestShow();
                } //"error"
                else {
                    lblAction.setText("Update fail.");
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        else 
        {
            lblAction.setText("All fields are required.");
        }
    }//GEN-LAST:event_btnUpdateActionPerformed

    private void btnExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExitActionPerformed
        // TODO add your handling code here:
        if (clientService != null && clientService.is_connected()) {
            this.close();
        }
        System.exit(0);
    }//GEN-LAST:event_btnExitActionPerformed

    private void btnRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRefreshActionPerformed
        // TODO add your handling code here:
        try {
            requestShow();            
        } catch(Exception e) {
            e.printStackTrace();
        }
    }//GEN-LAST:event_btnRefreshActionPerformed

    private void btnFindActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFindActionPerformed
        // TODO add your handling code here:
        if (!(txtID.getText().equals("")))
        {
            String xmlRequest;
            XMLParser xml = new XMLParser();

            xmlRequest  = xml.openTag("query");
            xmlRequest +=   xml.newItem("action","SEARCH");
            xmlRequest +=   xml.openTag("data");
            xmlRequest +=       xml.openTag("motor");
            xmlRequest +=           xml.newItem("id",txtID.getText());
            xmlRequest +=       xml.closeTag("motor");
            xmlRequest +=   xml.closeTag("data");
            xmlRequest += xml.closeTag("query");
            
            //call client's service - send()
            clientService.send(xmlRequest);

            //read data returned from Server
            try {
                //call client's service - receive()
                String xmlResponse = clientService.receive();                
                xml.parserXMLString(xmlResponse);
                
                if (xml.getValue("status").equals("ok")) {                    
                    txtID.setText(xml.getValue("id"));
                    txtName.setText(xml.getValue("name"));
                    txtColor.setText(xml.getValue("color"));
                    txtPrice.setText(xml.getValue("price"));
                    txtMark.setText(xml.getValue("mark"));
                    txtQuantity.setText(xml.getValue("quantity")); 
                    //
                    lblAction.setText("");
                } //"error"
                else {
                    //txtID.setText("");
                    txtName.setText("");
                    txtColor.setText("");
                    txtPrice.setText("");
                    txtMark.setText("");
                    txtQuantity.setText("");
                    //
                    lblAction.setText("No result");
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        else 
        {
            lblAction.setText("Motor ID is required.");
        }
    }//GEN-LAST:event_btnFindActionPerformed
    private void close() {
        try {
            String xmlRequest;
            XMLParser xml = new XMLParser();

            xmlRequest = xml.openTag("query");
            xmlRequest += xml.newItem("action", "CLOSE");
            xmlRequest += xml.closeTag("query");

            clientService.send(xmlRequest);
            clientService.disconnect();
        } catch (IOException ex) {
            lblError.setText("Cannot close it.");
        }
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ClientSide().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnConnect;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnDisconnect;
    private javax.swing.JButton btnExit;
    private javax.swing.JButton btnFind;
    private javax.swing.JButton btnRefresh;
    private javax.swing.JButton btnUpdate;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JLabel lblAction;
    private javax.swing.JLabel lblError;
    private javax.swing.JLabel lblStatus;
    private javax.swing.JMenu mEdit;
    private javax.swing.JMenu mFile;
    private javax.swing.JMenu mHelp;
    private javax.swing.JMenuItem miAbout;
    private javax.swing.JMenuItem miExit;
    private javax.swing.JTextField txtColor;
    private javax.swing.JTextField txtHost;
    private javax.swing.JTextField txtID;
    private javax.swing.JTextField txtMark;
    private javax.swing.JTextField txtName;
    private javax.swing.JTextField txtPort;
    private javax.swing.JTextField txtPrice;
    private javax.swing.JTextField txtQuantity;
    // End of variables declaration//GEN-END:variables
    
}
